package com.jwm.power.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.jwm.power.model.common.EnumOperate;
import com.jwm.power.model.common.EnumResult;
import com.jwm.power.model.entity.Logs;
import com.jwm.power.model.entity.SysDics;
import com.jwm.power.model.entity.Users;
import com.jwm.power.model.entity.Workordertypes;
import com.jwm.power.model.reponse.ResponseData;
import com.jwm.power.repository.LogsRepository;
import com.jwm.power.repository.WorkordertypesRepository;
import com.jwm.power.service.WorksService;
import com.jwm.power.util.Ut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.ServletRequestDataBinder;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Optional;

@Service
@Transactional
public class WorksServiceImpl implements WorksService {

    @Autowired
    private WorkordertypesRepository workordertypesRepository;
    @Autowired
    private LogsRepository logsRepository;

    //  设置工单类型
    @Override
    public ResponseData SaveOrderTypes(HttpServletRequest request) {
        try {
            Users users = (Users) StpUtil.getSession().get("users");
            Workordertypes model_New = new Workordertypes();
            ServletRequestDataBinder binder = new ServletRequestDataBinder(model_New);
            binder.bind(request);

            Integer operationType = 0;
            if (model_New.getTid() != null && model_New.getTid() > 0) {
                operationType = 1;
                Workordertypes workordertype = workordertypesRepository.findByTid(model_New.getTid());
                //  检查是否更工单类型名称
                if (!workordertype.getTname().equals(model_New.getTname())) {
                    List<Workordertypes> workordertypesList = workordertypesRepository.findByTnameAndTidNot(model_New.getTname(), model_New.getTid());
                    if (workordertypesList.size() > 0) {
                        return new ResponseData(EnumResult.WORK_FAIL_TNAME, model_New);
                    }
                }
            } else {
                String maxCode = workordertypesRepository.getMaxCode();
                maxCode = maxCode != null ? maxCode : "0";
                model_New.setTcode(String.format("%02d", Integer.parseInt(maxCode) + 1));
                //  判断同级下名称是否重复
                List<Workordertypes> workordertypesList = workordertypesRepository.findByTname(model_New.getTname());
                if (workordertypesList.size() > 0) {
                    return new ResponseData(EnumResult.WORK_FAIL_TNAME, model_New);
                }
            }
            workordertypesRepository.save(model_New);

            //  添加日志
            Logs log = Ut.SaveLog("ordertype", model_New.getTname(), operationType == 1 ? EnumOperate.EDIT : EnumOperate.ADD, users);
            logsRepository.save(log);
            return new ResponseData(EnumResult.SUCCESS, model_New);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseData(EnumResult.Exception.getCode(), e.getMessage(), null);
        }
    }

}
